using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using SqlSugar;
using System.Web.Http;
using wnet.admin.dbhelper;

using wnet.admin.Models;

using System;
using wnet.admin.wentcommon;
using Models;
using System.Web.UI.WebControls.WebParts;
using System.Net.NetworkInformation;


namespace wnet.admin.Controllers.ywBusiness
{
    /// <summary>
    /// 任务管理文档+pdf
    /// </summary>
    [TokenAuth]
    [Route("business/ywtaskmanage/{action}")]
    public class ywtaskmanageController : ApiController
    {

        private readonly ISqlSugarClient _sqlSugarClient;
        public ywtaskmanageController()
        {
            _sqlSugarClient = SqlSugarConfig.CreateClient();
        }
        /// <summary>
        /// 任务管理-列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpPost]
        public object mytasklist(yw_expertQueryDto parm)
        {
            int uid = tools.getuid();
            var predicate = Expressionable.Create<yw_taskmanage>();
            predicate = predicate.And(it => it.checkuserid == uid);
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.name), it => it.name.Contains(parm.name));
            var query = _sqlSugarClient.Queryable<yw_taskmanage>()
                .LeftJoin<sys_user>((it, su) => it.checkuserid == su.id)
                .Where(predicate.ToExpression())
                .Select((it, su) => new yw_taskmanage
                {
                    id = it.id.SelectAll(),
                    checkusername = su.name,
                });

            var datalist = query.ToPage(parm);

            var data = new { code = 200, msg = "成功", data = datalist };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }

        /// <summary>
        /// 任务管理-列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpPost]
        public object list(yw_expertQueryDto parm)
        {

            var predicate = Expressionable.Create<yw_taskmanage>();
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.name), it => it.name.Contains(parm.name));
            var query = _sqlSugarClient.Queryable<yw_taskmanage>()
                .LeftJoin<sys_user>((it, su) => it.checkuserid == su.id)
                .Where(predicate.ToExpression())
                .Select((it, su) => new yw_taskmanage
                {
                    id = it.id.SelectAll(),
                    checkusername = su.name,
                });

            var datalist = query.ToPage(parm);

            var data = new { code = 200, msg = "成功", data = datalist };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }

        /// <summary>
        /// 任务管理-详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public object Getyw_taskmanage(int id)
        {
            var response = _sqlSugarClient.Queryable<yw_taskmanage>()
                .Where(q => q.id == id).First();

            var data = new { code = 200, msg = "成功", data = response };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }

        /// <summary>
        /// 任务管理-添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public object Addyw_taskmanage([FromBody] yw_taskmanage parm)
        {

            parm.createtime = DateTime.Now;

            var response = _sqlSugarClient.Insertable<yw_taskmanage>(parm).ExecuteCommand();
            var data = new { code = 200, msg = "成功", data = response };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }


        /// <summary>
        /// 任务管理-编辑
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public object Updateyw_taskmanage([FromBody] yw_taskmanage parm)
        {

            parm.createtime = DateTime.Now;

            var response = _sqlSugarClient.Updateable<yw_taskmanage>(parm).ExecuteCommand();
            var data = new { code = 200, msg = "成功", data = response };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }

        /// <summary>
        /// 任务管理-确认
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public object updatequeren(int id)
        {
            var response = _sqlSugarClient.Queryable<yw_taskmanage>()
                .Where(q => q.id == id).First();
            response.confirmstatus = 1;
            _sqlSugarClient.Updateable<yw_taskmanage>(response).ExecuteCommand();
            var data = new { code = 200, msg = "成功", data = response };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }
        /// <summary>
        /// 任务管理-完成
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public object updatewancheng(int id)
        {
            var response = _sqlSugarClient.Queryable<yw_taskmanage>()
                .Where(q => q.id == id).First();
            response.status = 1;
            _sqlSugarClient.Updateable<yw_taskmanage>(response).ExecuteCommand();
            var data = new { code = 200, msg = "成功", data = response };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }
    }
}